﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.UpgradesUI {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Events.*;
    import Display.DialogUI.*;
    import org.aswing.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Classes.sim.*;
    import Display.*;
    import flash.text.*;
    import Display.aswingui.*;
    import Modules.stats.types.*;
    import L10n.*;

    public class BusinessUpgradesDialogView extends GenericDialogView {

        protected static const PROGRESS_BAR_WIDTH:int = 420;
        protected static const PROGRESS_BAR_MIN_WIDTH:int = 12;
        protected static const PROGRESS_TEXT_WIDTH:int = 170;
        protected static const MAX_LEVELS:int = 3;
        protected static const MAX_BATCH_COUNT:int = 20;
        protected static const MAX_BATCH_WAIT_TIME:int = 5;

        protected var m_building:MapResource;
        protected var m_finishCheckCB:Function;
        protected var m_finishCB:Function;
        protected var m_buyHandler:Function;
        protected var m_buttonsPanel:JPanel;
        protected var m_progressBarPane:JPanel;
        protected var m_progressBarBGPanel:JPanel;
        protected var m_progressLabel:JLabel;
        protected var m_buyButton:CustomButton;
        protected var m_upgradeCells:Array;
        protected var m_upgradeCellsContainer:JPanel;
        protected var m_buyBatchCount:int = 0;
        protected var m_buyBatchSequence:int = 0;

        public function BusinessUpgradesDialogView(_arg1:MapResource, _arg2:Function, _arg3:Function, _arg4:Function){
            this.m_upgradeCells = [];
            this.m_building = _arg1;
            this.m_finishCheckCB = _arg2;
            this.m_finishCB = _arg3;
            this.m_buyHandler = _arg4;
            super(null);
            this.setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
        }
        override protected function init():void{
            m_bgAsset = UpgradesCommonAssets.bizupsAssetDict["dialogBG"];
            this.makeBackground();
            this.makeCenterPanel();
            ASwingHelper.prepare(this);
        }
        override protected function makeBackground():void{
            var _local1:MarginBackground;
            if (m_bgAsset){
                _local1 = new MarginBackground(m_bgAsset);
                this.setBackgroundDecorator(_local1);
                ASwingHelper.setForcedWidth(this, m_bgAsset.width);
            };
        }
        override protected function makeCenterPanel():void{
            this.append(createHeaderPanel());
            this.append(ASwingHelper.verticalStrut(8));
            this.append(this.createUpgradeCellsPanel());
            this.append(ASwingHelper.verticalStrut(15));
            this.append(this.createProgressPanel());
            this.append(ASwingHelper.verticalStrut(15));
            this.append(this.createHelpersPanel());
            this.append(ASwingHelper.verticalStrut(25));
            this.append(this.createButtonsPanel());
        }
        override protected function createTitlePanel():JPanel{
            var _local2:String;
            var _local3:String;
            var _local4:String;
            var _local5:TextFormat;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            if (((this.m_building) && (this.m_building.canCountUpgradeActions()))){
                _local2 = Item.findUpgradeRoot(this.m_building.getItem());
                if (_local2){
                    _local3 = ZLoc.t("Items", (_local2 + "_friendlyName"));
                    _local4 = ZLoc.t("Dialogs", "UpgradeX", {itemName:_local3});
                    m_titleFontSize = TextFieldUtil.getLocaleFontSize(27, 20, null);
                    _local4 = TextFieldUtil.formatSmallCapsString(_local4);
                    title = ASwingHelper.makeTextField(_local4, EmbeddedArt.TITLE_FONT, m_titleFontSize, EmbeddedArt.titleColor, 3);
                    title.filters = EmbeddedArt.newtitleFilters;
                    _local5 = new TextFormat();
                    _local5.size = m_titleSmallCapsFontSize;
                    TextFieldUtil.formatSmallCaps(title.getTextField(), _local5);
                    _local1.append(title);
                    title.getTextField().height = (m_titleFontSize * 1.5);
                    ASwingHelper.setEasyBorder(_local1, 4);
                };
            };
            return (_local1);
        }
        protected function createUpgradeCellsPanel():JPanel{
            var _local3:BusinessUpgradesInfoBox;
            var _local5:Item;
            var _local6:JPanel;
            this.m_upgradeCellsContainer = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER, 10);
            var _local1:String = Item.findUpgradeRoot(this.m_building.getItem());
            var _local2:Array = UpgradeDefinition.getUpgradeChain(_local1);
            _local3 = new BusinessUpgradesInfoBox(UpgradesCommonAssets.bizupsAssetDict["currentCard"], _local1);
            this.m_upgradeCellsContainer.append(_local3);
            this.m_upgradeCells = new Array();
            this.m_upgradeCells.push(_local3);
            var _local4:int;
            while (_local4 < _local2.length) {
                _local5 = Global.gameSettings.getItemByName(_local2[_local4].newItemName);
                if (_local5.level <= this.m_building.getItem().level){
                    _local3 = new BusinessUpgradesInfoBox(UpgradesCommonAssets.bizupsAssetDict["currentCard"], _local5.name);
                } else {
                    _local3 = new BusinessUpgradesInfoBox(UpgradesCommonAssets.bizupsAssetDict["upgradeCard"], _local5.name);
                };
                _local6 = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
                _local6.append(new AssetPane((new (UpgradesCommonAssets.bizupsAssetDict["arrow"])() as DisplayObject)));
                this.m_upgradeCellsContainer.append(_local6);
                this.m_upgradeCellsContainer.append(_local3);
                this.m_upgradeCells.push(_local3);
                _local4++;
            };
            return (this.m_upgradeCellsContainer);
        }
        protected function createProgressPanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            var _local2:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.CENTER, 8);
            var _local3:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER, 2);
            var _local4:DisplayObject = (new (UpgradesCommonAssets.bizupsAssetDict["upgradeBarBG"])() as DisplayObject);
            this.m_progressBarBGPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
            ASwingHelper.setBackground(this.m_progressBarBGPanel, _local4);
            this.m_progressBarBGPanel.setPreferredWidth(PROGRESS_BAR_WIDTH);
            var _local5:DisplayObject = (new (UpgradesCommonAssets.bizupsAssetDict["upgradeBar"])() as DisplayObject);
            this.m_progressBarPane = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
            ASwingHelper.setBackground(this.m_progressBarPane, _local5);
            this.m_progressBarPane.setPreferredHeight(_local5.height);
            this.m_progressBarBGPanel.append(this.m_progressBarPane);
            this.updateProgressBar();
            _local3.append(this.m_progressBarBGPanel);
            var _local6:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.RIGHT);
            _local6.setPreferredWidth(this.m_progressBarBGPanel.getPreferredWidth());
            var _local7:AssetPane = ASwingHelper.makeMultilineText(ZLoc.t("Dialogs", "BusinessUpgradesUI_cashHint"), this.m_progressBarBGPanel.getPreferredWidth(), EmbeddedArt.DEFAULT_FONT_NAME_BOLD, TextFormatAlign.RIGHT, 16, EmbeddedArt.darkBlueTextColor);
            _local6.append(_local7);
            var _local8:AssetIcon = new AssetIcon((new EmbeddedArt.icon_cash() as DisplayObject));
            var _local9:int = UpgradesUtil.calculateCashCostPerUpgradeAction(this.m_building.getItem().commodityReq);
            this.m_buyButton = new CustomButton(_local9.toString(), _local8, "BigCashButtonUI");
            if (LocaleHelper.localSetting.sns.cash_enable){
                this.m_buyButton.addEventListener(MouseEvent.CLICK, this.onBuy, false, 0, true);
            };
            ASwingHelper.setForcedWidth(this.m_buyButton, 100);
            _local6.appendAll(ASwingHelper.verticalStrut(5), this.m_buyButton);
            if (this.m_finishCheckCB()){
                this.m_buyButton.setEnabled(false);
            };
            _local3.append(_local6);
            _local2.append(_local3);
            var _local10:DisplayObject = (new (UpgradesCommonAssets.bizupsAssetDict["verticleRule"])() as DisplayObject);
            var _local11:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            ASwingHelper.setBackground(_local11, _local10);
            _local11.setPreferredWidth(_local10.width);
            _local11.setPreferredHeight(_local2.getPreferredHeight());
            _local2.append(_local11);
            var _local12:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            this.m_progressLabel = ASwingHelper.makeLabel("", EmbeddedArt.TITLE_FONT, 30, EmbeddedArt.lightOrangeTextColor, JLabel.LEFT);
            this.updateProgressText();
            var _local13:AssetPane = ASwingHelper.makeMultilineCapsText(ZLoc.t("Dialogs", "BusinessUpgradesUI_action_text"), PROGRESS_TEXT_WIDTH, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, TextFormatAlign.LEFT, 12, EmbeddedArt.blueTextColor);
            _local12.appendAll(this.m_progressLabel, ASwingHelper.verticalStrut(-8), _local13);
            _local2.append(_local12);
            var _local14:DisplayObject = (new (UpgradesCommonAssets.bizupsAssetDict["divider"])() as DisplayObject);
            _local1.setBackgroundDecorator(new MarginBackground(_local14, new Insets(0, 9, 0, 10)));
            _local1.append(ASwingHelper.verticalStrut(5));
            _local1.append(_local2);
            _local1.append(ASwingHelper.verticalStrut(10));
            ASwingHelper.prepare(_local1);
            return (_local1);
        }
        protected function updateProgressBar():void{
            var _local1:int = this.m_building.upgradeActions.getTotal();
            var _local2:int = int(this.m_building.getItem().upgrade.requirements.getRequirementValue(Requirements.REQUIREMENT_UPGRADE_ACTIONS));
            var _local3:Number = Math.min(1, (_local1 / _local2));
            var _local4:int = ((_local1 == 0)) ? 0 : (PROGRESS_BAR_MIN_WIDTH + Math.ceil((_local3 * (this.m_progressBarBGPanel.getPreferredWidth() - PROGRESS_BAR_MIN_WIDTH))));
            this.m_progressBarPane.setPreferredWidth(_local4);
            ASwingHelper.prepare(this.m_progressBarBGPanel);
        }
        protected function updateProgressText():void{
            var _local1:int = this.m_building.upgradeActions.getTotal();
            var _local2:int = int(this.m_building.getItem().upgrade.requirements.getRequirementValue(Requirements.REQUIREMENT_UPGRADE_ACTIONS));
            _local1 = Math.min(_local1, _local2);
            this.m_progressLabel.setText(((("" + _local1) + "/") + _local2));
        }
        protected function createHelpersPanel():JPanel{
            var _local10:JPanel;
            var _local11:String;
            var _local12:JPanel;
            var _local13:JLabel;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            var _local2:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.CENTER, 10);
            var _local3:Array = this.m_building.upgradeActions.getFriendHelperIds();
            var _local4:UpgradeHelperDefinition = this.m_building.upgradeActions.getFriendHelperDefinition();
            if (!_local4){
                return (_local1);
            };
            var _local5:int = _local4.max;
            var _local6:Number = _local4.actionValue;
            var _local7:int;
            while (_local7 < _local5) {
                _local10 = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
                _local11 = null;
                if (_local3.length > _local7){
                    _local11 = _local3[_local7];
                };
                _local12 = this.makeImagePanel(_local11);
                _local10.append(_local12);
                _local13 = ASwingHelper.makeLabel(("+" + _local6), EmbeddedArt.TITLE_FONT, 16, EmbeddedArt.greenTextColor, JLabel.CENTER);
                _local10.append(_local13);
                _local2.append(_local10);
                _local7++;
            };
            _local1.append(_local2);
            var _local8:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local9:AssetPane = ASwingHelper.makeMultilineText(ZLoc.t("Dialogs", "BusinessUpgradesUI_hint"), _local2.getPreferredWidth(), EmbeddedArt.DEFAULT_FONT_NAME_BOLD, TextFormatAlign.LEFT, 16, EmbeddedArt.darkBlueTextColor);
            _local8.append(_local9);
            _local1.append(_local8);
            ASwingHelper.prepare(_local1);
            return (_local1);
        }
        protected function makeImagePanel(_arg1:String=null):JPanel{
            var imageAsset:* = null;
            var ap:* = null;
            var friend:* = null;
            var zid = _arg1;
            var jp:* = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            imageAsset = (new (UpgradesCommonAssets.bizupsAssetDict["blankPortrait"])() as DisplayObject);
            if (zid){
                friend = Global.player.findFriendById(zid);
                if (friend){
                    LoadingManager.load(friend.snUser.picture, function (_arg1:Event):void{
                        imageAsset = _arg1.target.content;
                        ap.setAsset(imageAsset);
                    });
                };
            };
            ap = new AssetPane(imageAsset);
            jp.append(ap);
            return (jp);
        }
        protected function createButtonsPanel():JPanel{
            var _local1:CustomButton;
            var _local2:CustomButton;
            var _local3:UpgradeHelperDefinition;
            if (this.m_buttonsPanel){
                this.m_buttonsPanel.removeAll();
            } else {
                this.m_buttonsPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            };
            if (this.m_finishCheckCB()){
                _local1 = new CustomButton(TextFieldUtil.formatSmallCapsString(ZLoc.t("Dialogs", "FinishBuilding")), null, "BigGreenButtonUI");
                _local1.addEventListener(MouseEvent.CLICK, this.onFinish, false, 0, true);
                ASwingHelper.setForcedHeight(_local1, 45);
                this.m_buttonsPanel.append(_local1);
            } else {
                _local2 = new CustomButton(TextFieldUtil.formatSmallCapsString(ZLoc.t("Dialogs", "ValUI_askforhelp")), null, "BigGreenButtonUI");
                _local2.addEventListener(MouseEvent.CLICK, this.onFinish, false, 0, true);
                ASwingHelper.setForcedHeight(_local2, 45);
                this.m_buttonsPanel.append(_local2);
                _local3 = this.m_building.upgradeActions.getFriendHelperDefinition();
                if (((!(_local3)) || ((this.m_building.upgradeActions.getFriendHelperIds().length >= _local3.max)))){
                    _local2.setEnabled(false);
                };
            };
            ASwingHelper.prepare(this.m_buttonsPanel);
            return (this.m_buttonsPanel);
        }
        protected function onBuy(_arg1:MouseEvent):void{
            var msg:* = null;
            var e:* = _arg1;
            if (this.m_finishCheckCB()){
                return;
            };
            var cashCost:* = UpgradesUtil.calculateCashCostPerUpgradeAction(this.m_building.getItem().commodityReq);
            if (!Global.player.getSeenSessionFlag("bizUpsCashBuy")){
                msg = ZLoc.t("Dialogs", "BusinessUpgradesUI_cashQuestion", {amount:cashCost});
                UI.displayMessage(msg, GenericDialogView.TYPE_YESNO, function (_arg1:GenericPopupEvent):void{
                    if (_arg1.button == GenericDialogView.YES){
                        Global.player.setSeenSessionFlag("bizUpsCashBuy");
                        onBuy(e);
                    };
                }, "", true);
                return;
            };
            if (Global.player.canBuyCash(cashCost)){
                Global.player.cash = (Global.player.cash - cashCost);
                this.m_building.incrementUpgradeActionCount();
                this.m_buyBatchCount++;
            } else {
                return;
            };
            if (this.m_buyBatchCount >= MAX_BATCH_COUNT){
                this.sendBatchedBuyTransaction();
            } else {
                this.startBatchTimer();
            };
            this.updateProgressBar();
            this.updateProgressText();
            StatsManager.sample(100, StatsCounterType.DIALOG, "businessupgradesui", "buy_one", this.m_building.getItemName());
            if (this.m_finishCheckCB()){
                this.m_buyButton.setEnabled(false);
                this.createButtonsPanel();
            };
        }
        private function startBatchTimer():void{
            var previousBatchCount:* = 0;
            var previousBatchSequence:* = 0;
            previousBatchCount = this.m_buyBatchCount;
            previousBatchSequence = this.m_buyBatchSequence;
            TimerUtil.callLater(function ():void{
                if (((((!((previousBatchCount == m_buyBatchCount))) || (!((previousBatchSequence == m_buyBatchSequence))))) || ((m_buyBatchCount == 0)))){
                    return;
                };
                sendBatchedBuyTransaction();
            }, (MAX_BATCH_WAIT_TIME * 1000));
        }
        private function sendBatchedBuyTransaction():void{
            GameTransactionManager.addTransaction(new TBuyUpgradeAction(this.m_building, this.m_buyBatchCount));
            this.m_buyBatchCount = 0;
            this.m_buyBatchSequence++;
        }
        protected function onFinish(_arg1:MouseEvent):void{
            if (this.m_buyBatchCount > 0){
                this.sendBatchedBuyTransaction();
            };
            this.m_finishCB(_arg1);
        }
        override protected function closeMe():void{
            if (this.m_buyBatchCount > 0){
                this.sendBatchedBuyTransaction();
            };
            super.closeMe();
        }
        override public function countDialogViewAction(_arg1:String="view", _arg2:Boolean=true, _arg3:int=1):void{
            if ((((m_doStatsTrackingFunction == null)) || ((m_doStatsTrackingFunction == true)))){
                return;
            };
            var _local4:Boolean = m_doStatsTrackingFunction();
            if (!_local4){
                return;
            };
            var _local5:String = m_getStatsTitleFunction();
            var _local6:String = GameUtil.trimBadStatsCharacters(_arg1);
            var _local7:String = (((this.m_building) && (this.m_building.hasOwnProperty("franchiseType")))) ? this.m_building["franchiseType"] : "";
            StatsManager.sample(100, StatsKingdomType.DIALOG_STATS, _local5, _local6, _local7);
            if (_arg2){
                m_acceptTextName = "";
                m_cancelTextName = "";
            };
        }

    }
}//package Display.UpgradesUI 
